package cn.tudu.boot051.controller;

import cn.tudu.boot051.entity.User;
import cn.tudu.boot051.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {
    @Autowired
    UserMapper mapper;
   @RequestMapping("/reg")
   //post请求且传递对象时要添加@RequestBody
   public int reg(@RequestBody User user){
       System.out.println("user = " + user);
       User u = mapper.selectByUsername(user.getUsername());
       if(u!=null){
           return 2;//用户名已存在
       }
       mapper.insert(user);
       return 1;//注册成功
   }

   @RequestMapping("/login")
    public int login(@RequestBody User user){
       User u = mapper.selectByUsername(user.getUsername());
       if(u!=null){
           if(user.getPassword().equals(u.getPassword())){
               return 1;
           }
           return 3;
       }
       return 2;

   }
}
